<!doctype html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../assert_selection.js"></script>
<script>
// Test to check if last and first word can be selected when they double-clicked
// https://bugs.webkit.org/show_bug.cgi?id=36359

function computePoint(selection) {
  const target = selection.document.getElementById('target');
  const width = target.offsetWidth;
  const height = target.offsetHeight;
  const left = selection.computeLeft(target);
  const top = selection.computeTop(target);
  return {x: left + width / 2, y: top + height / 2};

}

function doubleClick(selection) {
  resetMouse();
  const point = computePoint(selection);
  eventSender.mouseMoveTo(point.x, point.y);
  eventSender.mouseDown();
  eventSender.mouseUp();
  eventSender.mouseDown();
  eventSender.mouseUp();
}

function resetMouse() {
  if (!window.eventSender)
    throw 'This test requires eventSender.';
  eventSender.mouseMoveTo(0, 0);
  eventSender.mouseDown();
  eventSender.mouseUp();
}

function singleClickAndModify(selection) {
  resetMouse();
  const point = computePoint(selection);
  eventSender.mouseMoveTo(point.x, point.y);
  eventSender.mouseDown();
  eventSender.mouseUp();
  selection.modify('extend', 'backward', 'word');
  selection.collapseToStart();
  selection.modify('extend', 'forward', 'word');
}

// double click
selection_test(
    '<span contenteditable id="target">xyz</span> abc',
    selection => doubleClick(selection),
    '<span contenteditable id="target">^xyz|</span> abc',
    'double-click-1 first word');

selection_test(
    'abc <span contenteditable id="target">xyz</span>',
    selection => doubleClick(selection),
    'abc <span contenteditable id="target">^xyz|</span>',
    'double-click-2 last word');

selection_test(
    'abc <span contenteditable id="target">xyz</span> def',
    selection => doubleClick(selection),
    'abc <span contenteditable id="target">^xyz|</span> def',
    'double-click-3 middle word');

selection_test(
    '<div contenteditable><b id="target">xyz</b></div> abc',
    selection => doubleClick(selection),
    '<div contenteditable><b id="target">^xyz|</b></div> abc',
    'double-click-4 first block');

selection_test(
    'abc <div contenteditable><b id="target">xyz</b></div>',
    selection => doubleClick(selection),
    'abc <div contenteditable><b id="target">^xyz|</b></div>',
    'double-click-5 last block');

selection_test(
    'abc <div contenteditable><b id="target">xyz</b></div> def',
    selection => doubleClick(selection),
    'abc <div contenteditable><b id="target">^xyz|</b></div> def',
    'double-click-6 middle block');

// single click + modify
selection_test(
    '<span contenteditable id="target">xyz</span> abc',
    selection => singleClickAndModify(selection),
    '<span contenteditable id="target">|xyz</span> abc',
    'single-click-modify-1 first word');

selection_test(
    'abc <span contenteditable id="target">xyz</span>',
    selection => singleClickAndModify(selection),
    'abc <span contenteditable id="target">^xyz|</span>',
    'single-click-modify-2 last word');

selection_test(
    'abc <span contenteditable id="target">xyz</span> def',
    selection => singleClickAndModify(selection),
    'abc <span contenteditable id="target">|xyz</span> def',
    'single-click-modify-3 middle word');

selection_test(
    '<div contenteditable><b id="target">xyz</b></div> abc',
    selection => singleClickAndModify(selection),
    '<div contenteditable><b id="target">^xyz|</b></div> abc',
    'single-click-modify-4 first block');

selection_test(
    'abc <div contenteditable><b id="target">xyz</b></div>',
    selection => singleClickAndModify(selection),
    'abc <div contenteditable><b id="target">^xyz|</b></div>',
    'single-click-modify-5 last block');

selection_test(
    'abc <div contenteditable><b id="target">xyz</b></div> def',
    selection => singleClickAndModify(selection),
    'abc <div contenteditable><b id="target">^xyz|</b></div> def',
    'single-click-modify-6 middle block');
</script>
